package LeetCode;

import java.util.Arrays;

/**
 * @author Twelve
 */
public class swapFunction {

    public static void swap(int[] arr, int i, int j) {
        if (i != j) {
            //当i == j的时候，arr[i] = arr[i] ^ arr[j]会使arr[i/j]这里的值变为0，即arr[i]==arr[j]==0
            arr[i] ^= arr[j];
            arr[j] ^= arr[i];
            arr[i] ^= arr[j];
        }
    }
    public static void main(String[] args) {
        int[] arr = {1,2,2,4,5};
        swap(arr,1,2);
        System.out.println(Arrays.toString(arr));
    }
}
